package com.ysq.nfd.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 时间工具类
 * @author admin
 *
 */
public class DateUtil {

	/**
	 * 毫秒时间戳 + 随机数
	 * @return string 时间戳
	 */
	public static String getNanoRandom() {
		long time = System.nanoTime();
		Random random = new Random();
		time += random.nextInt(999999999);
		return String.valueOf(time);
	}
	
	/**
	 * 得到当前时间时间
	 * @param 时间格式
	 * @return string 按格式返回当前时间
	 */
	public static int getCurrentDay() {
		long time = System.currentTimeMillis();
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int day = Integer.parseInt(sdf.format(date));
		return day;
	}
	
	/**
	 * 获得当前日期
	 * @param formatType
	 * @return
	 */
	public static String getNowTime(String formatType){
		
		Date date = new Date();
		
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		
		String resultDay = format.format(date);
		
		return resultDay;
	}
	
	/**
	 * 转换String 为 date
	 * @param dateStr
	 * @param formatType
	 * @return
	 * @throws Exception
	 */
	public static Date parseDate(String dateStr, String formatType) throws Exception{
		
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		
		Date date = format.parse(dateStr);
		
		return date;
	}
	
	/**
	 * 将 2014-01-11 这样的日期转换成 20140211
	 * @param day
	 * @return
	 */
	public static long parseDayToLong(String day){
		long longDay = new Long(day.replaceAll("-", ""));
		
		return longDay;
	}
	
	/**
	 * 获得字符串日期时间戳
	 * @param day
	 * @param formatType
	 * @return
	 * @throws Exception
	 */
	public static long getDateTime(String day, String formatType) throws Exception{
		
		Date date = DateUtil.parseDate(day, formatType);
		
		long timeLong = date.getTime();
		
		return timeLong;
	}
	
	/**
	 * 日期转字符串
	 * @param date
	 * @param formatType
	 * @return
	 */
	public static String dateToString(Date date, String formatType){
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		
		String resultDay = format.format(date);
		return resultDay;
	}
	
	public static void main(String[] args) {
		
		String dateStr = "2016-06-28 10:19:34";
		String formatType = "yyyy-MM-dd HH:mm:ss";
		
		
		
		Date day;
		try {
			day = parseDate(dateStr, formatType);
			
			long l = day.getTime();
			
			System.out.println(l);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		System.out.println("####LastHourDay : ");
	}

}
